package com.hollycrm.unicom.sheet.business.logic.hibernate;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.ConfirmSheet;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.domain.MainSheet;
import com.hollycrm.unicom.sheet.domain.SheetAppu;
import com.hollycrm.unicom.sheet.flow.SheetAction;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class HibernateDeleteAppuLogic
    extends HibernateDaoSupport
    implements ActionExecuteLogic {
    public HibernateDeleteAppuLogic() {
    }

    public void execute(AuthToken authToken,SheetAction action, DoOperateContext context) throws
        Exception {
        HibernateTemplate ht = getHibernateTemplate();
        String formFileName = (String) context.getInputAttr("formFileName");
        String[] rowIDs = (String[])context.getInputAttr("rowIDs");
        String[] savedNames = new String[rowIDs.length];
        String sheetFlowNO = null;
        Object sheet = null;
        for(int i=0;i<rowIDs.length;i++){
        SheetAppu appu=(SheetAppu)ht.load(SheetAppu.class,rowIDs[i]);
        updateAppuCount(ht,appu,context);
        if(i==0)
            sheetFlowNO = appu.getSheetFlowNO();
        savedNames[i]=appu.getSavedName();
        ht.delete(appu);
        }

        context.addOutputAttr("dealType","DELETE");
        context.addOutputAttr("savedNames",savedNames);
        context.addOutputAttr("sheetFlowNO",sheetFlowNO);
//        context.addOutputAttr("mainSheetFlowNO", mainSheetFlowNO);
//        context.addOutputAttr("operationObject", appu.getSheetFlowNO());
    }

    private String updateAppuCount(HibernateTemplate ht,
                                   SheetAppu appu, DoOperateContext context) throws Exception {
        String flowNO = null;
        if (appu.getSheetType().equals(Constants.SHEET_TYPE_MAIN)) {
            MainSheet sheet = (MainSheet) ht.load(MainSheet.class,
                                                  appu.getSheetFlowNO());
            flowNO = sheet.getSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue() - 1));
            ht.update(sheet);
            context.addOutputAttr(sheet);
        }
        else if (appu.getSheetType().equals(Constants.SHEET_TYPE_DEAL)) {
            DealSheet sheet = (DealSheet) ht.load(DealSheet.class,
                                                  appu.getSheetFlowNO());
            flowNO = sheet.getMainSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue() - 1));
            ht.update(sheet);
            context.addOutputAttr(sheet.getMainSheet());
        }
        else if (appu.getSheetType().equals(Constants.SHEET_TYPE_CONFIRM)) {
            ConfirmSheet sheet = (ConfirmSheet) ht.load(ConfirmSheet.class,
                appu.getSheetFlowNO());
            flowNO = sheet.getMainSheetFlowNO();
            sheet.setAppuCount(new Integer(sheet.getAppuCount().intValue() - 1));
            ht.update(sheet);
            context.addOutputAttr(sheet.getMainSheet());
        }
        return flowNO;
    }
}
